using System;
using System.Collections;
using System.Data;
using System.Collections.Generic;

namespace ASPNETVN.PORTAL.Components.Configuration.Items
{
    public class Portal
    {
        private Guid _ID;
        private string _Title;
        private string _Slogan;
        private string _Layout = "Office";
        private string _DefaultTheme = "";
        private string _Language = "en-US";
        private bool _AlwaysShowEditButton;
        private bool _AllowRegister = true;
        private string _WebMasterName;
        private string _WebMasterEmail;
        private string _WebMasterPhone;

        private Tab _ActiveTab = new Tab();
        private IList<Tab> _Tabs = new List<Tab>();

        public Portal(Guid tabID)
        {
            try
            {
                // Get the configuration data.
                PortalDataSet ds = PortalConfiguration.GetPortalDataSet();
                
                // Get portal ID from web.config
                Guid portalID = new Guid(System.Configuration.ConfigurationManager.AppSettings["PortalID"]);

                // Read the Tab Information, and sort by Tab Order
                DataRow[] tabRows = ds.Tab.Select("PortalID ='" + portalID + "'", "SortOrder");
                // If no tabs.
                if (tabRows.Length == 0) return;
                
                foreach (PortalDataSet.TabRow tRow in tabRows)
                {
                    Tab tab = new Tab();
                    tab.ID = tRow.ID;
                    tab.PortalID = tRow.PortalID;
                    tab.ParentID = tRow.ParentID;
                    tab.Name = tRow.Name;
                    tab.AuthorizedRoles = tRow.AuthorizedRoles;
                    tab.SortOrder = tRow.SortOrder;
                    tab.ThemeName = tRow.ThemeName;
                    tab.ShowLeftPane = tRow.ShowLeftPane;
                    tab.ShowRightPane = tRow.ShowRightPane;
                    tab.ShowOnMenu = tRow.ShowOnMenu;
                    this.Tabs.Add(tab);
                }

                // If the Portal.ActiveTab property is set to 0, change it to  
                // the TabID of the first tab in the DesktopTabs collection
                if (this._ActiveTab.ID == Guid.Empty)
                    this._ActiveTab.ID = ((Tab) this.Tabs[0]).ID;


                // Read the Module Information for the current (Active) tab
                PortalDataSet.TabRow activeTab = ds.Tab.FindByID(tabID);

                // Get Modules for this Tab based on the Data Relation
                foreach (PortalDataSet.ModuleRow moduleRow in activeTab.GetModuleRows())
                {
                    Module module = Module.LoadModuleRow(moduleRow);
                    module.TabID = tabID;
                    this.ActiveTab.Modules.Add(module);
                }

                // Sort the modules in order of ModuleOrder
                // this.ActiveTab.Modules.Sort();

                // Get the first row in the Global table
                PortalDataSet.PortalRow globalSettings = (PortalDataSet.PortalRow)ds.Portal.Rows[0];

                // Read Portal global settings 
                this._ID = globalSettings.ID;
                this._Title = globalSettings.Title;
                this._Slogan = globalSettings.Slogan;
                this._AlwaysShowEditButton = globalSettings.AlwaysShowEditButton;
                this._Layout = globalSettings.Layout;
                this._DefaultTheme = globalSettings.DefaultTheme;
                this._Language = globalSettings.Language;
                this._AllowRegister = globalSettings.AllowRegister;
                this._WebMasterName = globalSettings.WebMasterName;
                this._WebMasterEmail = globalSettings.WebMasterEmail;
                this._WebMasterPhone = globalSettings.WebMasterPhone;

                this._ActiveTab.ID = tabID;
                this._ActiveTab.Name = activeTab.Name;
                this._ActiveTab.AuthorizedRoles = activeTab.AuthorizedRoles;
                this._ActiveTab.SortOrder = activeTab.SortOrder;
                this._ActiveTab.ThemeName = activeTab.ThemeName;
                this._ActiveTab.ShowLeftPane = activeTab.ShowLeftPane;
                this._ActiveTab.ShowRightPane = activeTab.ShowRightPane;
                this._ActiveTab.ShowOnMenu = activeTab.ShowOnMenu;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Portal ID.
        /// </summary>
        public Guid ID
        {
            get { return this._ID; }
            set { this._ID = value; }
        }

        /// <summary>
        /// Portal Name.
        /// </summary>
        public string Title
        {
            get { return this._Title; }
            set { this._Title = value; }
        }

        public bool AlwaysShowEditButton
        {
            get { return this._AlwaysShowEditButton; }
            set { this._AlwaysShowEditButton = value; }
        }

        public IList<Tab> Tabs
        {
            get { return this._Tabs; }
            set { this._Tabs = value; }
        }

        public Tab ActiveTab
        {
            get { return this._ActiveTab; }
            set { this._ActiveTab = value; }
        }

        public string Layout
        {
            get { return this._Layout; }
            set { this._Layout = value; }
        }

        public string Language
        {
            get { return this._Language; }
            set { this._Language = value; }
        }

        public bool AllowRegister
        {
            get { return this._AllowRegister; }
            set { this._AllowRegister = value; }
        }

        public string DefaultTheme
        {
            get { return this._DefaultTheme; }
            set { this._DefaultTheme = value; }
        }

        public string Slogan
        {
            get { return _Slogan; }
            set { _Slogan = value; }
        }

        public string WebMasterName
        {
            get { return _WebMasterName; }
            set { _WebMasterName = value; }
        }

        public string WebMasterEmail
        {
            get { return _WebMasterEmail; }
            set { _WebMasterEmail = value; }
        }

        public string WebMasterPhone
        {
            get { return _WebMasterPhone; }
            set { _WebMasterPhone = value; }
        }
    }
}